package com.wzq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.controller.admin.GetSome;
import com.wzq.dao.pojo.Appointment;
import com.wzq.dao.pojo.DoorProject;
import com.wzq.dao.pojo.Project;
import com.wzq.dao.pojo.Wechat_user;
import com.wzq.util.DateUtil;
import com.wzq.util.StringUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/entryGuard")
public class EntrGuardController extends GetSome {
    @PostMapping("/authentication")
    public String authentication(@RequestBody String param) throws Exception {
        System.out.println(param);
        String[] params = param.split("&");
        Map<String, String> map = new HashMap<String, String>();
        for (String s : params) {
            if (s.contains("=")) {
                String[] temp = s.split("=");
                if (temp.length >= 2) {
                    map.put(temp[0], temp[1]);
                }
            }
        }
        String openid = map.get("vgdecoderesult");
        String p_number = map.get("devicenumber");
        // 获得openid对应的 用户id
        Wechat_user user = iWechat_userService.getOne(new LambdaQueryWrapper<Wechat_user>().select(Wechat_user::getId).eq(Wechat_user::getOpenid, openid));
        LambdaQueryWrapper<Appointment> lqw = new LambdaQueryWrapper<>();

        // 判断该用户是否为管理员
        for (String s : StringUtil.Stringp()) {
            if (user!=null && s.equals(openid)){          //
                return "code=" + "0000";
            }
        }


        //region 判断用户是否预约门
        Integer p_id = iProjectService.getOne(new LambdaQueryWrapper<Project>().eq(Project::getP_number, p_number)).getId();
        lqw.eq(Appointment::getW_id,user.getId()).eq(Appointment::getP_id,p_id).eq(Appointment::getPass, 1)
                .eq(Appointment::getIsover, 0).le(Appointment::getBegin_time, DateUtil.getCurrentDatePath("yyyy-MM-dd HH:mm"));
        if (iAppointmentService.count(lqw)>0) {
            return "code=" + "0000";
        }
        //endregion

        //region 没有预约门  通过门的设备号查询 门下所有的 设备id 封装并 准备查询
        List<DoorProject> list = iDoorProjectService.list(new LambdaQueryWrapper<DoorProject>().
                eq(DoorProject::getP_number, p_number).select(DoorProject::getP_id));
        // 如果没有设备关联这个门 则返回失败
        if(list.size()<1){
            return "code=" + "0001";
        }
        Integer[] ids = new Integer[list.size()];
        int i = 0;
        for (DoorProject doorProject : list) {
            ids[i++] = doorProject.getP_id();
        }
        //endregion

        //region 查询该用户是否预约 该门下的设备  若在预约时间内 则返回成功
        //更新预约表  保证预约最新状态
        appointmentover();
        // 更新查询条件
        lqw.clear();
        lqw.eq(Appointment::getW_id, user.getId()).in(Appointment::getP_id, ids).eq(Appointment::getPass, 1)
                .eq(Appointment::getIsover, 0).le(Appointment::getBegin_time, DateUtil.getCurrentDatePath("yyyy-MM-dd HH:mm"));
        if (iAppointmentService.getOne(lqw) != null) {
            return "code=" + "0000";
        }
        //endregion
        // 返回失败
        return "code=" + "0001";
    }
}